package fr.kraml.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import fr.kraml.LoginFragment;
import fr.kraml.RegisterFragment;

public class TabsPagerAdapter extends FragmentPagerAdapter implements
		ViewPager.OnPageChangeListener, TabListener {

	public List<Fragment> frags = new ArrayList<Fragment>();
	public ViewPager mPager;
	public ActionBar mActionBar;
	public Fragment mPrimaryItem;

	public TabsPagerAdapter(FragmentManager fm) {
		super(fm);
		// TODO Auto-generated constructor stub
	}

	public TabsPagerAdapter(FragmentManager fm, ViewPager vp, ActionBar ab) {
		super(fm);
		mPager = vp;
		mPager.setAdapter(this);
		mPager.setOnPageChangeListener(this);
		mActionBar = ab;
	}

	public void addFragment(Fragment f, String text) {
		frags.add(f);
		this.notifyDataSetChanged();
		mActionBar.addTab(mActionBar.newTab().setTabListener(this)
				.setText(text));
		
	}

	@Override
	public Fragment getItem(int index) {
		// TODO Auto-generated method stub
		/*
		 * switch (index) { case 0: // Top Rated fragment activity return new
		 * LoginFragment(); case 1: // Games fragment activity return new
		 * RegisterFragment(); } return null;
		 */
		return frags.get(index);
	}

	public void removeAllFragments() {
		frags.clear();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return frags.size();
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.FragmentStatePagerAdapter#setPrimaryItem(android.view.ViewGroup,
	 *      int, java.lang.Object)
	 */
	@Override
	public void setPrimaryItem(ViewGroup container, int position, Object object) {
		super.setPrimaryItem(container, position, object);
		mPrimaryItem = (Fragment) object;
	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.view.PagerAdapter#getItemPosition(java.lang.Object)
	 */
	@Override
	public int getItemPosition(Object object) {
		if (object == mPrimaryItem) {
			return POSITION_UNCHANGED;
		}
		return POSITION_NONE;
	}

	@Override
	public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction arg1) {
		// TODO Auto-generated method stub
		mPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		mActionBar.setSelectedNavigationItem(position);
	}
	
	public void removePages() {
        mActionBar.removeAllTabs();

                    //call to ViewPage to remove the pages
        mPager.removeAllViews();
        frags.clear();

        //make this to update the pager
        mPager.setAdapter(null);
        mPager.setAdapter(this);
    }
	
	@Override
	public void destroyItem(View pView, int pIndex, Object pObject) {
	        ((ViewPager) pView).removeView((View)pObject);
	}
	
}
